package dao

import (
	"fmt"
	__ "user-srv/basic/proto"
	"user-srv/handler/models"
)

// 会员权益添加
func MemberLevelAdd(in *__.MemberLevelAddReq) (*models.MemberLevel, error) {
	var memberLevel models.MemberLevel
	var memberBenefit models.MemberBenefit

	err := memberBenefit.GetMemberBenefitInfoById(int(in.BenefitId))
	if err != nil {
		return nil, fmt.Errorf("会员权益查找失败")
	}
	if memberBenefit.Id == 0 {
		return nil, fmt.Errorf("会员权益不存在")
	}

	memberLevel.BenefitId = in.BenefitId  //会员权益id
	memberLevel.Name = in.Name            //会员名称
	memberLevel.Price = float64(in.Price) //会员价格
	memberLevel.Level = in.Level          //会员等级
	memberLevel.Desc = in.Desc            //描述
	err = memberLevel.MemberLevelAdd()
	if err != nil {
		return nil, fmt.Errorf("会员等级添加失败")
	}
	return &memberLevel, nil
}

// 会员等级展示
func MemberLevelFind(in *__.MemberLevelFindReq) ([]*__.MemberLevelFind, error) {
	var MemberLevel models.MemberLevel
	var memberLevelList []*__.MemberLevelFind

	memberLeve, err := MemberLevel.MemberLevelFind(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, fmt.Errorf("会员等级查询失败")
	}

	for _, find := range memberLeve {
		memberLevelList = append(memberLevelList, &__.MemberLevelFind{
			Id:        find.Id,
			BenefitId: find.BenefitId,
			Name:      find.Name,
			Price:     find.Price,
			Level:     find.Level,
			Desc:      find.Desc,
		})
	}
	return memberLevelList, nil
}

// 会员等级详情
func MemberLevelDetail(in *__.MemberLevelDetailReq) (*models.MemberLevel, error) {
	var memberLevel models.MemberLevel
	memberLevel.Id = in.Id
	err := memberLevel.MemberLevelFindById(int(in.Id))
	if err != nil {
		return nil, fmt.Errorf("会员等级查询失败")
	}
	if memberLevel.Level == 0 {
		return nil, fmt.Errorf("会员等级不存在")
	}

	return &memberLevel, nil
}

// 会员等级删除
func MemberLevelDel(in *__.MemberLevelDelReq) (*models.MemberLevel, error) {
	var memberLevel models.MemberLevel
	memberLevel.Id = in.Id
	err := memberLevel.MemberLevelDel()
	if err != nil {
		return nil, fmt.Errorf("会员等级删除失败")
	}

	return &memberLevel, nil
}
